import Vue from 'vue'
import Vuex from 'vuex'
import {getCateList} from '../api/home'
import {getUserInfo} from '../api/user' 
import {getUploadToken,uploadQiniu} from "../api/api";
Vue.use(Vuex)
const CHANGE = 'change';

import {Toast} from 'vant'

export default new Vuex.Store({
  state: {
    a:1,
    isLogin:false,
    uid:"",
    token:"",
    userInfo:[],
    cate:[],
    publish_num:0,
    liked_num:0
  },
  getters:{

  },
  mutations: {
    change(state,val) {
      state.a = val
    },
    [CHANGE](state,{key,value}) {
      state[key] = value
    },
    clear(state) {
      state.uid = '',
      state.token = '',
      state.userInfo = {},
      state.isLogin = false;
      state.publish_num = 0;
      state.liked_num = 0
    }
  },
  actions: {
   async getuserInfo({commit},token) {
     console.log(111);
     return getUserInfo({
        token
      }).then(res=>{
        console.log(res)
        if(res.code == 0) {
          // commit('change',{key:"userInfo",value:res.userInfo})
          // commit('change',{key:"uid",value:res.uid})
          // commit('change',{key:"token",value:res.token})
          // commit('change',{key:"isLogin",value:res.isLogin})
          commit('change', {
            key: "userInfo",
            value: res.userInfo
          })
          commit('change', {
            key: "uid",
            value: res.uid
          })
          commit('change', {
            key: "token",
            value: token
          })
          commit('change', {
            key: "isLogin",
            value: true
          })
          commit('change', {
            key: "publish_num",
            value: res.publish_num
          })
          commit('change', {
            key: "liked_num",
            value: res.liked_num
          })
          
        }
        else {
          Toast(res.msg)
        }
        
      })
    },
    getCate({state,
      commit
    }) {
      getCateList().then(res => {
        console.log(res)
        commit('change', {
          key: 'cate',
          value: res.data
        })
      })

    },
 
    async upload(o,fileList) {
      let imageSrc = [];
      if (!fileList || !Array.isArray(fileList) || fileList.length === 0) {
 
        return imageSrc;
      }

      let task = fileList.map((v) => {
        return new Promise(async (resolve, reject) => {
          let {
            file
          } = v;
          let {
            type
          } = file;
    
          type = type.split("/")[1];

          let file_name = `${new Date().getTime()}_${Math.random()
                .toString(36)
                .slice(2)}.${type}`;
          

          let {
            token
          } = await getUploadToken();

          console.log(token);

          const formdata = new FormData();
          formdata.append("file", file);
          formdata.append("token", token);
          formdata.append("key", file_name);
          console.log(file);
          console.log(token);
          console.log(file_name);
          uploadQiniu(formdata).then((res) => {
            console.log(res);
            resolve(`http://toutiao.longxiaokj.com/` + res.key);
          });
        });
      });

      // task  [promises,promise]

      imageSrc = await Promise.all(task);

      return imageSrc;
    },

  },
  modules: {}
})